package com.mcarrot.site.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mcarrot.site.model.User;
import com.mcarrot.site.service.UserService;


public class UserAction extends BaseAction {

	private static final long serialVersionUID = 5555693945413508223L;
	
	private int start;
	private int limit;
	
	private UserService userService;
	
	private Log log = LogFactory.getLog(getClass());

	public String forward(){
		return SUCCESS;
	}
	
	public void list(){
		List<User> users = new ArrayList<User>();
		start = 0;
		limit = 5;
		Integer count = userService.getCount(null, null, null);
		users = userService.listUsers(null, null, null, start, limit);
		
		try {
			JSONArray jsonArray = JSONArray.fromObject(users);
			JSONObject json = new JSONObject();
			json.accumulate("totalNum", count);
			json.accumulate("list", jsonArray.toString());
			PrintWriter pw = getResponse().getWriter();
			log.info(getClass().getName() + " --> jsonArry.toString[list()]: " +jsonArray.toString() );
			log.info(getClass().getName() + " --> json.toString[list()]: " +json.toString() );
			pw.write(json.toString());
		} catch (IOException e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(getClass().getName() + "--->", e);
		}
		
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
